using UnityEngine;
using System.Collections;
public class Cam : MonoBehaviour
{
    public float maxdistance = 5.0f;
    public float xSpeed = 120.0f;
    public float ySpeed = 120.0f;

    public float yMinLimit = -90f;
    public float yMaxLimit = 90f;

    float x = 0.0f;
    float y = 0.0f;
    public float yoffset = -3;
    public static Cam _This;
    void Awake()
    {
        _This = this;
    }
    void Start()
    {

        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }
    public bool spectator;
    void LateUpdate()
    {


        if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Tab)) Screen.lockCursor = !Screen.lockCursor;

        if (Screen.lockCursor)
        {
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        }
        GameObject pl = GameObject.Find("Player");
        Quaternion rotation = Quaternion.Euler(y, x , 0);
        Vector3 pos = pl.transform.position;
        Vector3 pos2 = rotation * new Vector3(0.0f, 0.0f, -maxdistance) + pos;
        pos2.y -= yoffset;
        transform.position = pos2;
        transform.rotation = rotation;

    }
    internal float ran;
    public static float ClampAngle(float angle, float min, float max, float clamp)
    {
        if (angle < -clamp)
            angle = -clamp;
        if (angle > clamp)
            angle = clamp;
        return Mathf.Clamp(angle, min, max);
    }


}